package pac;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.VisitorSupport;
import org.dom4j.io.SAXReader;


public class XMLUtils {
	public static final String STATUSCODE = "StatusCode";
	
	public static final String TAG_PLANE = "plane";
	public static final String TAG_PLACE = "place";
	public static final String TAG_USER_ID = "user_id";
	public static final String TAG_PASSWORD = "password";
	public static final String TAG_LONGTITUDE = "longtitude";
	public static final String TAG_LATITUDE = "latitude";
	public static final String TAG_NEWPASSWORD = "npass";
	public static final String TAG_NEWUSERINFO = "newinfo";
	public static final String TAG_USERNAME = "name";
	public static final String TAG_USER = "user";
	public static final String TAG_EMAIL = "email";
	public static final String TAG_REPLY = "reply";
	public static final String TAG_LETTER_ID = "letter_id";
	public static final String TAG_MESSAGE = "message";
	public static final String TAG_SENDPLANE = "send";
	public static final String TAG_RADIUS = "radius";
	public static final String TAG_PAPER_ID = "paper_id";
	public static final String TAG_PLANE_TYPE = "type";
	public static final String TAG_DATE = "date";
	public static final String TAG_GENDER = "gender";
	public static final String TAG_BIRTHDAY = "birthday";
	public static final String TAG_REGISTER = "register";
	public static final String TAG_SENDID = "sender_id";
	public static final String TAG_RECEIVERID = "receiver_id";
	public static final String TAG_VALIDDATE = "vdate";
	
	
	/**
	 * ��XML�����������ݶ�ȡ������Ŀǰֻ�ܶ�ȡ�����XML����ΪĿǰ��Э�������Ѿ��㹻
	 * @param stream ������
	 * @return Ϊ������չ������List������ʵ��ֻ��Ҫ��һ��Ԫ�ؼ��ɡ�
	 * @throws DocumentException �ĵ���ȡ�쳣
	 */
	public static List<Map<String, String>> parseXML( InputStream stream ) throws DocumentException{
		List<Map<String , String>> list = new ArrayList<Map<String,String>>();
		Map<String, String> items= new HashMap<String, String>();

		SAXReader reader = new SAXReader();
		Document document = reader.read(stream);
		XMLVistor vistor = new XMLVistor(items);
		document.accept(vistor);
		list.add(items);
		return list;
	}
	

	public static class XMLVistor extends VisitorSupport{
		Map<String, String> items;
		public XMLVistor( Map<String, String> map ){
			items = map;
		}
		@Override
		public void visit( Element node ){
			items.put( node.getName(), node.getText());
		}
	}
	
	public static Document changeToXML( String rootName, String[] paramNames, String[] values) {
		Document document = DocumentHelper.createDocument();
		Element root = document.addElement( rootName);
		for( int i=0; i<paramNames.length; i++ ){
			root.addElement( paramNames[i] ).addText( values[i]);
		}
		return document;
	}
	
}
